// package com.vguang.usbplugin;
package com.vguang;


import android.util.Log;


public class UsbPlugin extends UniModule{
	private static final String TAG = "UsbPlugin";
	 @UniJSMethod(uiThread = true)
	 
	 public class VbarSo {
	 	static
	 	{
	 
	 		System.loadLibrary("channel");
	 	}
	 
	 	/**
	 	 *
	 	 * @param type
	 	 * @param arg
	 	 * @return
	 	 */
	 	public native static long vbar_channel_open(int type, byte[] arg);
	 
	 	/**
	 	 *
	 	 * @param channel
	 	 * @param buffer
	 	 * @param length
	 	 * @return
	 	 */
	 	public native static int vbar_channel_send(long channel, byte[] buffer, int length);
	 
	 	/**
	 	 *
	 	 * @param channel
	 	 * @param buffer
	 	 * @param size
	 	 * @param milliseconds
	 	 * @return
	 	 */
	 	public native static int vbar_channel_recv(long channel, byte[] buffer, int size, int milliseconds);
	 
	 	/**
	 	 *
	 	 * @param channel
	 	 */
	 	public native static void vbar_channel_close(long channel);
	 
	 	//初始化设备变量
	 	public static long vbar_channel = 0;
	 
	 
	 	//连接设备
	 	public boolean vbarOpen() {
	 		if (vbar_channel == 0) {
	 			vbar_channel = vbar_channel_open(1, "0".getBytes());
	 		}
	 		if (vbar_channel != 0) {
	 			System.out.println("connect device success");
	 			return true;
	 		} else {
	 			System.out.println("connect device fail");
	 			return false;
	 		}
	 	}
	 
	 	//背光控制
	 	public void vbarLight(boolean lightstate) {
	 		if (vbar_channel != 0) {
	 			byte[] buffer = new byte[1024];
	 			byte[] buffer1 = new byte[1024];
	 			int i = 0;
	 			if (lightstate) {
	 				buffer[i] = 0x55;
	 				buffer[++i] = (byte) 0xAA;
	 				buffer[++i] = 0x24;
	 				buffer[++i] = 0x01;
	 				buffer[++i] = 0x00;
	 				buffer[++i] = 0x01;
	 				buffer[++i] = (byte) 0xDB;
	 				for (int j = 7; j < 64; j++) {
	 					buffer[j] = 0x00;
	 				}
	 			}
	 			else {
	 				buffer[i] = 0x55;
	 				buffer[++i] = (byte) 0xAA;
	 				buffer[++i] = 0x24;
	 				buffer[++i] = 0x01;
	 				buffer[++i] = 0x00;
	 				buffer[++i] = 0x00;
	 				buffer[++i] = (byte) 0xDA;
	 			}
	 			vbar_channel_send(vbar_channel, buffer, 64);
	 			vbar_channel_recv(vbar_channel, buffer1, 64, 200);
	 		}
	 	}
	 
	 	//扫码开关
	 	public void controlScan(boolean scanswitch) {
	 		if(vbar_channel != 0)
	 		{
	 			byte[] buffer = new byte[1024];
	 			byte[] buffer1 = new byte[1024];
	 			int i = 0;
	 			if (scanswitch) {
	 				buffer[i] = 0x55;
	 				buffer[++i] = (byte) 0xAA;
	 				buffer[++i] = 0x05;
	 				buffer[++i] = 0x01;
	 				buffer[++i] = 0x00;
	 				buffer[++i] = 0x00;
	 				buffer[++i] = (byte) 0xFB;
	 			}
	 			 else {
	 				buffer[i] = 0x55;
	 				buffer[++i] = (byte) 0xAA;
	 				buffer[++i] = 0x05;
	 				buffer[++i] = 0x01;
	 				buffer[++i] = 0x00;
	 				buffer[++i] = 0x01;
	 				buffer[++i] = (byte) 0xFA;
	 			}
	 			vbar_channel_send(vbar_channel, buffer, 64);
	 			vbar_channel_recv(vbar_channel, buffer1, 64, 200);
	 		}
	 	}
	 
	 
	 	//接收结果
	 	public String getResultsingle() {
	 		byte[] bufferrecv = new byte[1024];
	 
	 		vbar_channel_recv(vbar_channel, bufferrecv, 1024, 200);
	 		if (bufferrecv[0] == 85 && bufferrecv[1] == -86 && bufferrecv[3] == 0x00) {
	 			int datalen = (bufferrecv[4] & 0xff) + (bufferrecv[5] << 8);  //二维码数据总长度
	 			if (datalen > 0) {
	 				byte[] readBuffers = new byte[datalen];
	 				for (int s1 = 0; s1 < datalen; s1++)
	 				{
	 					readBuffers[s1] = bufferrecv[6 + s1];
	 				}
	 				String str = new String(readBuffers);
	 				return str;
	 			}
	 			else
	 			{
	 				return null;
	 			}
	 		} else {
	 			return null;
	 		}
	 	}
	 }
}





	
	

